@using BTCPayServer.Services.Apps
@using BTCPayServer.Client
@model ListAppsViewModel
@inject AppService AppService
@{
    ViewData.SetLayoutModel(new(nameof(AppsNavPages), "Apps"));
    var nextAppNameSortOrder = (string)ViewData["AppNameNextSortOrder"];
    var nextAppTypeSortOrder = (string)ViewData["AppTypeNextSortOrder"];
    var nextStoreNameSortOrder = (string)ViewData["StoreNameNextSortOrder"];
    var appNameSortOrder = nextAppNameSortOrder switch
    {
        "asc" => "desc",
        "desc" => "asc",
        _ => null
    };
    var appTypeSortOrder = nextAppTypeSortOrder switch
    {
        "asc" => "desc",
        "desc" => "asc",
        _ => null
    };
    var storeNameSortOrder = nextStoreNameSortOrder switch
    {
        "asc" => "desc",
        "desc" => "asc",
        _ => null
    };
    const string sortByDesc = "Sort by descending...";
    const string sortByAsc = "Sort by ascending...";
}

<partial name="_StatusMessage" />

<div class="sticky-header">
    <h2>
        @ViewData["Title"]
        <small>
            <a href="https://docs.btcpayserver.org/Apps/" target="_blank" rel="noreferrer noopener" title="@StringLocalizer["More information..."]">
                <vc:icon symbol="info" />
            </a>
        </small>
    </h2>
	<a id="page-primary" asp-action="CreateApp" asp-route-storeId="@Context.GetStoreData().Id" class="btn btn-primary" role="button" text-translate="true">Create a new app</a>
</div>

<div class="table-responsive-md">
    @if (Model.Apps.Any())
    {
        <table class="table table-hover">
            <thead>
            <tr>
                <th>
                    <a
                        asp-action="ListApps"
                        asp-route-storeId="@Context.GetStoreData().Id"
                        asp-route-sortOrder="@(nextAppNameSortOrder ?? "asc")"
                        asp-route-sortOrderColumn="AppName"
                        class="text-nowrap"
                        title="@(appNameSortOrder == "desc" ? sortByDesc : sortByAsc)"
                    >
                        <span text-translate="true">Name</span>
                        <vc:icon symbol="actions-sort-alpha-@(appNameSortOrder ?? nextAppNameSortOrder ?? "desc")" />
                    </a>
                </th>
                <th>
                    <a
                        asp-action="ListApps"
                        asp-route-storeId="@Context.GetStoreData().Id"
                        asp-route-sortOrder="@(nextAppTypeSortOrder ?? "asc")"
                        asp-route-sortOrderColumn="AppType"
                        class="text-nowrap"
                        title="@(appTypeSortOrder == "desc" ? sortByDesc : sortByAsc)"
                    >
                        <span text-translate="true">App Type</span>
                        <vc:icon symbol="actions-sort-alpha-@(appTypeSortOrder ?? nextAppTypeSortOrder ?? "desc")" />
                    </a>
                </th>
                <th>
                    <a
                        asp-action="ListApps"
                        asp-route-storeId="@Context.GetStoreData().Id"
                        asp-route-sortOrder="@(nextStoreNameSortOrder ?? "asc")"
                        asp-route-sortOrderColumn="StoreName"
                        class="text-nowrap"
                        title="@(storeNameSortOrder == "desc" ? sortByDesc : sortByAsc)"
                    >
                        <span text-translate="true">Store</span>
                        <vc:icon symbol="actions-sort-alpha-@(storeNameSortOrder ?? nextStoreNameSortOrder ?? "desc")" />
                    </a>
                </th>
            </tr>
            </thead>
            <tbody>
            @foreach (var app in Model.Apps)
            {
                var appType = AppService.GetAppType(app.AppType)!;
                var appData = new AppData { Id = app.Id, StoreDataId = app.StoreId, Name = app.AppName, AppType = app.AppType };
                var url = await appType.ConfigureLink(appData);
                <tr>
                    <td>
                        <a href="@url" permission="@Policies.CanModifyStoreSettings" id="App-@app.Id">@app.AppName</a>
                        <span not-permission="@Policies.CanModifyStoreSettings">@app.AppName</span>
                        @if (app.Archived)
                        {
                            <span class="badge bg-info ms-2" text-translate="true">archived</span>
                        }
                    </td>
                    <td>
                        @AppService.GetAvailableAppTypes()[app.AppType]
                        @{
                            var viewStyle = @app.ViewStyle;
                        }
                        @if (!string.IsNullOrEmpty(viewStyle))
                        {
                            <span>-</span>
                            <span>@viewStyle</span>
                        }
                    </td>
                    <td>
                        <span permission="@Policies.CanModifyStoreSettings">
                            <a asp-controller="UIStores" asp-action="GeneralSettings" asp-route-storeId="@app.StoreId">@app.StoreName</a>
                        </span>
                        <span not-permission="@Policies.CanModifyStoreSettings">@app.StoreName</span>
                    </td>
                </tr>
            }
            </tbody>
        </table>
    }
    else
    {
        <p class="text-secondary mt-3" text-translate="true">
            There are no apps yet.
        </p>
    }
</div>

<partial name="_Confirm" model="@(new ConfirmModel("Delete app", "This app will be removed from this store.", StringLocalizer["Delete"]))" />
